﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Wp7Tester.ServiceReference1;
//using Wp7Tester.LocalServiceReference;

namespace Wp7Tester
{
    public partial class MainPage : PhoneApplicationPage
    {

        SchedulerServiceClient client;
        EventHandler<TestFunCompletedEventArgs> TestFunCompletedEventHandler;
        EventHandler<AddTaskCompletedEventArgs> AddTaskCompletedEventHandler;
        EventHandler<RemoveTaskCompletedEventArgs> RemoveTaskCompletedEventHandler;
        EventHandler<GetTasksDueSoonCompletedEventArgs> ListCompletedEventHandler;
        EventHandler<GetNearbyLocationsCompletedEventArgs> NearbyCompletedEventHandler;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            TestFunCompletedEventHandler = new EventHandler<TestFunCompletedEventArgs>(client_TestFunCompleted);
            AddTaskCompletedEventHandler = new EventHandler<AddTaskCompletedEventArgs>(client_AddTaskCompleted);
            RemoveTaskCompletedEventHandler = new EventHandler<RemoveTaskCompletedEventArgs>(client_RemoveTaskCompleted);
            ListCompletedEventHandler = new EventHandler<GetTasksDueSoonCompletedEventArgs>(client_ListCompleted);
            NearbyCompletedEventHandler = new EventHandler<GetNearbyLocationsCompletedEventArgs>(client_NearbyCompleted);
            client = new SchedulerServiceClient();
            guids[0] = new Guid("45da0861-4157-4d22-8f40-b7bcd9162917");
            guids[1] = new Guid("4bc95a90-125f-4de3-a17d-26adc1f007c5");
            guids[2] = new Guid("4d3841f1-307b-48c9-8ce5-6d10a61b0601");
            guids[3] = new Guid("877b6203-beb1-4c93-88d5-01f83acac7f6");
            guids[4] = new Guid("880404b9-61ba-4105-b499-3a473910881b");
            guids[5] = new Guid("a1490803-61c8-4990-b973-51fa5eb0bae1");
            guids[6] = new Guid("c0f8ff49-05d9-4d33-a6c6-44ca26976129");
            guids[7] = new Guid("fc5e9021-0887-4e11-ae1a-e3461659fcbb");
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            client.TestFunCompleted += TestFunCompletedEventHandler;
            client.TestFunAsync();
        }

        void client_TestFunCompleted(object sender, TestFunCompletedEventArgs e)
        {
            PageTitle.Text = e.Result.ToString();
            client.TestFunCompleted -= TestFunCompletedEventHandler;
        }

        void client_AddTaskCompleted(object sender, AddTaskCompletedEventArgs e)
        {
            PageTitle.Text = e.Result.ToString();
            client.AddTaskCompleted -= AddTaskCompletedEventHandler;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Task TheTask = new Task();
            TheTask.Id = Guid.NewGuid();
            TheTask.LocationType = "";
            TheTask.Name = "";
            TheTask.Notes = "";
            TheTask.ReminderTime = DateTime.Now;
            TheTask.Deadline = DateTime.Now + new TimeSpan(2, 40, 0);
            client.AddTaskCompleted += AddTaskCompletedEventHandler;
            client.AddTaskAsync(1, 2, TheTask);
        }

        static int i = 0;
        Guid[] guids = new Guid[8];

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            client.RemoveTaskCompleted += RemoveTaskCompletedEventHandler;
            client.RemoveTaskAsync(guids[i++]);
        }

        void client_RemoveTaskCompleted(object sender, RemoveTaskCompletedEventArgs e)
        {
            PageTitle.Text = e.Result.ToString();
            client.RemoveTaskCompleted -= RemoveTaskCompletedEventHandler;
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            System.Collections.ObjectModel.ObservableCollection<long> O = new System.Collections.ObjectModel.ObservableCollection<long>();
            client.GetTasksDueSoonCompleted += client_ListCompleted;
            client.GetTasksDueSoonAsync(O);
        }

        void client_ListCompleted(object sender, GetTasksDueSoonCompletedEventArgs e)
        {
            PageTitle.Text = e.Result.ToString();
            client.GetTasksDueSoonCompleted -= ListCompletedEventHandler;
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            client.GetNearbyLocationsCompleted += NearbyCompletedEventHandler;
            client.GetNearbyLocationsAsync(new GeoCoordinate(45, -93), 38);
        }

        void client_NearbyCompleted(object sender, GetNearbyLocationsCompletedEventArgs e)
        {
            PageTitle.Text = e.Result.Count.ToString();
            client.GetNearbyLocationsCompleted -= NearbyCompletedEventHandler;
        }
    }
}